#include "myhead.h"
int main()
{
    //创建一个套接字
    int ssock= socket(AF_INET,SOCK_STREAM,0);
    
    int opt=1;
    setsockopt(ssock,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));

    struct sockaddr_in myaddr;
    myaddr.sin_family=AF_INET;
    myaddr.sin_addr.s_addr=INADDR_ANY;
    myaddr.sin_port=htons(10000);
    bind(ssock,(struct sockaddr *)&myaddr,sizeof(myaddr));

    listen(ssock,10);

    struct sockaddr_in youaddr;
    int size = sizeof(youaddr);
    fd_set myset;
    FD_ZERO(&myset);
    int newsock = accept(ssock,(struct sockaddr *)&youaddr,&size);
    char buf[10]="";
    printf("接受成功%d\n",newsock);
    while(1)
    {
        FD_SET(0,&myset);
        FD_SET(newsock,&myset);
        bzero(buf,sizeof(buf));
        int ret = select(newsock+1,&myset,NULL,NULL,NULL);//返回值：大于0成功，小于0失败，等于0超时
        if(ret <0)
        {
            perror("检测失败\n");
            close(ssock);
            return -1;
        }
        if(ret ==0)
        {
            perror("超时\n");
            close(ssock);
            return -1;
        }
        if(FD_ISSET(newsock,&myset))
        {
            recv(newsock,buf,sizeof(buf),0);
            printf("%d发来%s\n",newsock,buf);
        }
        if(FD_ISSET(0,&myset))
        {
            scanf("%9s",buf);
            send(newsock,buf,strlen(buf),0);
        }
    }
    
}